<template>
  <div>
    <el-dialog title="菜单" :visible.sync="openAddDialog.switch">
      <el-form :model="form" class="items-in-form" ref="addForm">
        <div class="right-radio">
          <span class="right-title">权限组名称</span>
          <el-radio v-model="radio" label="菜单"></el-radio>
          <el-radio v-model="radio" label="权限点"></el-radio>
        </div>
        <el-form-item label="权限组名称" :label-width="formLabelWidth">
          <select-tree
            v-model="form.selected"
            :options="menuList"
            :props="defaultProps"
            @getParentId="getParentId"
          />
        </el-form-item>
        <div class="normal-input">
          <el-form-item label="权限代码" :label-width="formLabelWidth">
            <el-input v-model="form.code" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="权限标题" :label-width="formLabelWidth">
            <el-input v-model="form.title" autocomplete="off"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('closeDialog')">取 消</el-button>
        <el-button type="primary" @click="addMenuHandle('addForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import selectTree from '@/utils/components/select-tree'
import { menuList, addMenu } from '@/api/menu'
export default {
  name: 'addDialog',
  components: {
    selectTree
  },
  props: {
    openAddDialog: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      menuList: [],
      // 数据默认字段
      defaultProps: {
        parent: 'pid', // 父级唯一标识
        value: 'code', // 唯一标识
        label: 'title', // 标签显示
        children: 'childs', // 子级
        is_point: 'is_point'
      },
      radio: '菜单',
      form: {
        code: '',
        title: '',
        selected: '',
        pid: '',
        is_point: false
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    // 获取列表的数据
    async getMenuList() {
      try {
        const res = await menuList()
        const newRes = JSON.parse(
          JSON.stringify(res.data).replace(/points/g, 'childs')
        )
        this.menuList = newRes
      } catch (err) {}
    },
    getParentId(pid) {
      this.form.pid = pid
    },
    async addMenuHandle(addForm) {
      try {
        await addMenu(this.form)
        this.$message({
          type: 'success',
          message: '添加成功'
        })
        this.$emit('closeDialog')
        this.getMenuList()
        this.$emit('refresh')
        const form = {
          code: '',
          title: '',
          selected: '',
          pid: '',
          is_point: false
        }
        this.form = form
      } catch (err) {
        this.$message({
          type: 'info',
          message: '添加失败'
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-dialog__header {
  background-color: #409eff;
  .el-dialog__title,
  .el-dialog__close {
    color: #fff;
  }
}
.items-in-form {
  .right-radio {
    margin-bottom: 30px;
    .right-title {
      margin: 0 10px 0 38px;
    }
  }
  /deep/ .el-input--suffix {
    width: 300px;
  }
  /deep/ .el-popper {
    width: 300px;
  }
  .normal-input /deep/ .el-input__inner {
    width: 300px;
  }
}
</style>
